<?php
/**
 * 企业 联系方式控制类
 *
 * */
class EcontactusAction extends Action {
    /**
     * 管理所有的联系方式
     * */
    public function AdminIndex(){
        EasyACL('Econtactus','AdminIndex',$_SESSION);
        $Econtactus = D('Econtactus');
        //计算所有的记录
        $count = $Econtactus->count(NULL,'e_id');
        //每页显示的数量
        $listRows = 15;
        //需要显示的列   并关联获取
        $fields = 'e_id,enter_id,enterprise_name,created';
        import('ORG.Util.Page');
        $p = new Page($count,$listRows);
        $list = $Econtactus->xfindall(NULL,$fields,'e_id desc',$p->firstRow.','.$p->listRows);
        $page = $p->show();

        //设置到模板
        $this->assign('Contacts',$list);
        $this->assign('page',$page);
        $this->display();
    }

    /**
     * 创建联系方式
     * */
    public function AdminCreate(){
        EasyACL('Econtactus','AdminCreate',$_SESSION);
        //POST
        if($this->isPost()){
            $Input = Input::getInstance();
            $Input->filter('addslashes');
            
            //设置查询条件
            import('ORG.Util.HashMap');
            $conditions = new HashMap();
            $conditions->put('enter_id',array('EQ',$Input->post('enter_id')));

            $EContact = D('Econtactus');
            $count = $EContact->count($conditions,'e_id');
            if($count >= 1) {
                halt('该企业已有自己的联系方式，请选择修改!');
            }
            $EContact = NULL;

            $Econtact = D('Econtactus');
            $Econtact->enter_id = $Input->post('enter_id');
            $Econtact->enterprise_name = $Input->post('enterprise_name');
            $Econtact->enterprise_tel = $Input->post('enterprise_tel');
            $Econtact->enterprise_fax = $Input->post('enterprise_fax');
            $Econtact->enterprise_add = $Input->post('enterprise_add');
            $Econtact->enterprise_mail = $Input->post('enterprise_mail');
            $Econtact->enterprise_msn = $Input->post('enterprise_msn');
            $Econtact->enterprise_qq = $Input->post('enterprise_qq');
            $Econtact->enterprise_phone = $Input->post('enterprise_phone');
            $Econtact->enterprise_code = $Input->post('enterprise_code');
            $RunTime = time();
            $Econtact->created = $RunTime;
            $Econtact->updated = $RunTime;
            //echo $Input->post('enter_id');
            
            $result = $Econtact->add();
            if(!$result) {
                halt('尝试创建联系方式失败');
            }
            $this->redirect('AdminIndex','Econtactus');
        }
        //显示创建页面
        $this->display();
    }

    /**
     * 修改联系方式
     * */
    public function AdminUpdate()
    {
        EasyACL('Econtactus','AdminUpdate',$_SESSION);

        $Input = Input::getInstance();
        $Input->filter('addslashes');

        import('ORG.Util.HashMap');
        $conditions = new HashMap();
        //POST
        if($this->isPost())
        {
            $conditions->put('e_id',array('EQ',$Input->post('e_id')));
            $Econtact = D('Econtactus');
            $Econtact->where($conditions)->find();
            $Econtact->enterprise_name = $Input->post('enterprise_name');
            $Econtact->enterprise_tel = $Input->post('enterprise_tel');
            $Econtact->enterprise_fax = $Input->post('enterprise_fax');
            $Econtact->enterprise_add = $Input->post('enterprise_add');
            $Econtact->enterprise_mail = $Input->post('enterprise_mail');
            $Econtact->enterprise_msn = $Input->post('enterprise_msn');
            $Econtact->enterprise_qq = $Input->post('enterprise_qq');
            $Econtact->enterprise_phone = $Input->post('enterprise_phone');
            $Econtact->enterprise_code = $Input->post('enterprise_code');
            $Econtact->updated = time();
            $result = $Econtact->save();
            if (!$result) {
                halt('尝试修改信息失败');
            }
            $this->redirect('AdminIndex','Econtactus');
        }
        ///获取要修改的联系方式
        $conditions->put('e_id',array('EQ',$Input->get('e_id')));
        $Econtact = D('Econtactus');
        $Contact = $Econtact->where($conditions)->find();

        //设置到模板文件
        $this->assign('Contact',$Contact);
        $this->display();
    }
    /**
     * 删除联系方式
     * */
    public function AdminDel($e_id){
        EasyACL('Econtactus','AdminDel',$_SESSION);
        $Econtact = D('Econtactus');
        $result = $Econtact->deleteById($e_id);
        if(!$result) {
            return FALSE;
        } else {
            return TRUE;
        }
    }
    public function AdminDels(){
        EasyACL('Econtactus','AdminDels',$_SESSION);
        if($this->isPost()) {
            $eids = $_POST['e_id'];
            $Error = '';
            foreach ($eids as $id) {
                $result = $this->AdminDel($id);
                if(!$result){
                    $Error .= '尝试删除编号'.$id.'联系方式失败!';
                }
            }
            if(empty($Error)) {
                $this->redirect('AdminIndex','Econtactus');
            } else {
                halt($Error);
            }
        }

        if($this->isGet()) {
            $Input = Input::getInstance();
            $Input->filter('addslashes');
            $result = $this->AdminDel($Input->get('e_id'));
            if(!$result) {
                halt('尝试删除联系方式失败!');
            }
            $this->redirect('AdminIndex','Econtactus');
        }
    }
}
